package com.oracle.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.gson.internal.$Gson$Preconditions;
import com.oracle.compents.ApiResponse;
import com.oracle.pojo.Equipment;
import com.oracle.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/equipment")
public class EquipmentController {

    @Autowired
    private EquipmentService equipmentService;

    @PostMapping("/findEquipment")
    public ApiResponse findEquipment(@RequestBody Equipment equipment, Integer pageNum , Integer pageSize) {
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        String equipmentName = equipment.getEquipmentName();
        Integer state = equipment.getState();
        queryWrapper.eq("state",state);
        if (equipmentName != null && !equipmentName.isEmpty()) {
            queryWrapper.like("equipment_Name", equipmentName);
            queryWrapper.orderByDesc("equipment_Id");
            IPage<Equipment> equipmentPage1 = equipmentService.findEquipmentPage(pageNum, pageSize,queryWrapper);
            if (equipmentPage1 != null && !equipmentPage1.getRecords().isEmpty()) {
                // 查询结果不为空，进行相应处理

                ApiResponse<IPage<Equipment>> success = ApiResponse.successIPage(equipmentPage1);

                success.setMsg("查询成功");

                return success;
            } else {
                ApiResponse<Object> failed = ApiResponse.failed(null);
                failed.setMsg("体育器材模块，查询不到器材");
                return failed;
            }
        } else {
            queryWrapper.orderByDesc("equipment_Id");
            IPage<Equipment> equipmentPage1 = equipmentService.findEquipmentPage(pageNum, pageSize, queryWrapper);
            if (equipmentPage1 != null && !equipmentPage1.getRecords().isEmpty()) {
                // 查询结果不为空，进行相应处理

                ApiResponse<IPage<Equipment>> success = ApiResponse.successIPage(equipmentPage1);

                success.setMsg("查询成功");

                return success;
            } else {
                ApiResponse<Object> failed = ApiResponse.failed(null);
                failed.setMsg("体育器材模块，查询不到记录");
                return failed;
            }
        }
    }


    @GetMapping("/findOne")
    public ApiResponse findOne(Integer id){
        Equipment oneEquipment = equipmentService.findOneEquipment(id);
        if (null!=oneEquipment){
            ApiResponse<Equipment> success = ApiResponse.success(oneEquipment);
            success.setMsg("查询成功");
            return success;
        }else {
            ApiResponse<Object> failed = ApiResponse.failed(null);
            failed.setMsg("体育器材模块，查询失败");
            return failed;
        }
    }

    @PostMapping("/addEquipment")
    public ApiResponse addEquipment(@RequestBody Equipment equipment){
        equipment.setState(1);
        int i = equipmentService.insertEquipment(equipment);
        if (i>0){
            ApiResponse<IPage<Equipment>> success = ApiResponse.successIPage(null);
            success.setMsg("添加成功");
            return success;
        }else {
            ApiResponse<Object> failed = ApiResponse.failed(null);
            failed.setMsg("体育器材模块，添加失败");
            return failed;
        }
    }


    @PostMapping("/updataEquipment")
    public ApiResponse updataEquipment(@RequestBody Equipment equipment){
        int b = equipmentService.updataEquipment(equipment);
        if (b>0){
            ApiResponse<IPage<Equipment>> success = ApiResponse.successIPage(null);
            success.setMsg("修改成功");
            return success;
        }else {
            ApiResponse<Object> failed = ApiResponse.failed(null);
            failed.setMsg("体育器材模块，修改失败");
            return failed;
        }
    }


    @PostMapping("/updataState")
    public ApiResponse updataState(@RequestBody Equipment equipment){
        int i = equipmentService.updataState(equipment);
        if (i>0){
            ApiResponse<IPage<Equipment>> success = ApiResponse.successIPage(null);
            success.setMsg("修改成功");
            return success;
        }else {
            ApiResponse<Object> failed = ApiResponse.failed(null);
            failed.setMsg("体育器材模块，修改失败");
            return failed;
        }
    }


    @GetMapping("/deleteEquipment")
    public ApiResponse deleteEquipment(Integer id){
        int a = equipmentService.deleteEquipment(id);
        if (a>0){
            ApiResponse<IPage<Equipment>> success = ApiResponse.successIPage(null);
            success.setMsg("删除成功");
            return success;
        }else {
            ApiResponse<Object> failed = ApiResponse.failed(null);
            failed.setMsg("体育器材模块，删除失败");
            return failed;
        }
    }


    @GetMapping("/findAllEquipment")
    public ApiResponse findAllEquipment(){
        List<Equipment> allEquipment = equipmentService.findAllEquipment();
        if (ObjectUtils.isEmpty(allEquipment)){
            return ApiResponse.failed(null);
        }else {
            return ApiResponse.success(allEquipment);
        }
    }



}